﻿using Demo_MVVM基本框架.Service;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using WinForm = System.Windows.Forms;

namespace Demo_MVVM基本框架.View
{
    /// <summary>
    /// UserGetExcelData.xaml 的交互逻辑
    /// </summary>
    public partial class UserGetExcelData : UserControl
    {
        public UserGetExcelData()
        {
            InitializeComponent();
            this.DataContext = ViewModel.ViewModel.GetInstance().m_Model_MainWindow;
            ViewModel.ViewModel.GetInstance().m_Model_MainWindow.LClientItem = new ObservableCollection<string>();
            ViewModel.ViewModel.GetInstance().m_Model_MainWindow.LClientItem.Add("123");
        }

        string strWriteFilePath = string.Empty;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //WinForm.FolderBrowserDialog m_Dialog = new WinForm.FolderBrowserDialog();
            //m_Dialog.Description = "打开文件";
            //if (m_Dialog.ShowDialog() == WinForm.DialogResult.OK)
            //    if (m_Dialog.SelectedPath != string.Empty)
            //    {
            //        strFilePath = m_Dialog.SelectedPath;
            //    }

            // ViewModel.ViewModel.GetInstance().m_Model_MainWindow.LClientItem.Add("321");

            TT();
            //ReadExcel();
        }

        private void ReadExcel()
        {
            string strReadFilePath = string.Empty;

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = "All File|*.*|Excel documents|*.xlsx|Excel(97 - 2003)documents|*.xls"; //eg: Excel documents|*.xlsx | Excel documents|*.xlsx
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                strReadFilePath = dlg.FileName;
            }
            else
                return;

            System.IO.FileStream fs = new System.IO.FileStream(strReadFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            IWorkbook workbook = WorkbookFactory.Create(fs);

            //获取一共多少张表
            int nSheets = workbook.NumberOfSheets;

            for (int i = 0; i < nSheets; i++)
            {
                //获取表格名字
                string strSheetName = workbook.GetSheetName(i);

                if (i != 0)
                {
                    //1.读取Excel表格中的第 i 张Sheet表
                    ISheet sheet = workbook.GetSheetAt(i);

                    //获取一共多少行
                    int nRowsCount = sheet.LastRowNum + 1;

                    string strAim = "0000";
                    for (int k = 0; k < nRowsCount; k++)
                    {
                        // 第I行的所有内容
                        IRow row = sheet.GetRow(k);
                        //第I行 第0列的内容
                        ICell cell = row.GetCell(0);
                        strAim += string.Format(",{0}", cell.ToString());
                    }
                    //CsINIofKeyBinding.GetInstance().WriteValue(strSheetName, "对比码", strAim);
                }
            }
        }

        private void WriteExcel()
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.Filter = "Excel documents|*.xlsx|Excel(97 - 2003)documents|*.xls";
            bool? result = dlg.ShowDialog();
            if (result == true)
                strWriteFilePath = dlg.FileName;
            else
                return;

            System.IO.FileStream fs = new System.IO.FileStream(strWriteFilePath, System.IO.FileMode.Create);

            IWorkbook workbook;
            if (strWriteFilePath.Contains(".xls"))
                workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
            else if (strWriteFilePath.Contains(".xlsx"))
                workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
            else
                return;

            int nRow = 5;
            int nCell = 30;

            //新建第一张表
            ISheet sheet = workbook.CreateSheet();
            //新建第 nRow 行数据
            IRow row = sheet.CreateRow(nRow);
            //向第 nRow 行 第 nCell 列 添加数据“序号” 
            row.CreateCell(nCell).SetCellValue("序号");
            //写入表格
            workbook.Write(fs);
            //关闭表格 
            fs.Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            WriteExcel();
        }

        private void TextBox_PasswordChanged(object sender, RoutedEventArgs e)
        {

        }

        private void TT()
        {
            List<string> LAim = new List<string>() { "ANSI", "JIS", "ISO" };
            List<string[]> LCompare = new List<string[]>();
            for (int i = 0; i < 3; i++)
            {
                var strGet = CsINIofKeyBinding.GetInstance().Read4CValue(LAim[i], "对比码");
                var ddd = strGet.Length;
                var strCompareData = CsINIofKeyBinding.GetInstance().Read4CValue(LAim[i], "对比码").Split(',');//本地ini
                LCompare.Add(strCompareData);
            }
        }
    }
}
